package com.deep.sftp;


import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
/**
 * @author: chenly
 * @date: 2022-11-01 11:41
 * @description:
 * @version: 1.0
 */
public class SftpPooledObjectFactory extends BasePooledObjectFactory<SftpClientObject> {

    private final SftpProperties sftpProperties;

    public SftpPooledObjectFactory(SftpProperties sftpProperties) {
        this.sftpProperties = sftpProperties;
    }

    @Override
    public SftpClientObject create() throws Exception {
        return new SftpClientObject(sftpProperties);
    }

    @Override
    public PooledObject<SftpClientObject> wrap(SftpClientObject sftpClient) {
        return new DefaultPooledObject<>(sftpClient);
    }

    @Override
    public boolean validateObject(PooledObject<SftpClientObject> p) {
        return p.getObject().validateConnect();
    }

    @Override
    public void destroyObject(PooledObject<SftpClientObject> p) {
        p.getObject().disconnect();
    }

}